package com.wheat.vipcard.bean;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;

/**
 * A data access object (DAO) providing persistence and search support for
 * Tribune entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.wheat.vipcard.bean.Tribune
 * @author MyEclipse Persistence Tools
 */

public class TribuneDAO extends BaseHibernateDAO {
	private static final Log log = LogFactory.getLog(TribuneDAO.class);
	// property constants
	public static final String TNAME = "tname";
	public static final String TCONTENT = "tcontent";
	public static final String TCREATE_TIME = "tcreateTime";
	public static final String TGOOD = "tgood";
	public static final String TBAD = "tbad";
	public static final String USER_ID = "userId";
	public static final String USER_NAME = "userName";
	public static final String TTYPE = "ttype";

	public void save(Tribune transientInstance) {
		log.debug("saving Tribune instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Tribune persistentInstance) {
		log.debug("deleting Tribune instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Tribune findById(java.lang.Integer id) {
		log.debug("getting Tribune instance with id: " + id);
		try {
			Tribune instance = (Tribune) getSession().get(
					"com.wheat.vipcard.bean.Tribune", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Tribune instance) {
		log.debug("finding Tribune instance by example");
		try {
			List results = getSession().createCriteria(
					"com.wheat.vipcard.bean.Tribune").add(
					Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Tribune instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Tribune as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByTname(Object tname) {
		return findByProperty(TNAME, tname);
	}

	public List findByTcontent(Object tcontent) {
		return findByProperty(TCONTENT, tcontent);
	}

	public List findByTcreateTime(Object tcreateTime) {
		return findByProperty(TCREATE_TIME, tcreateTime);
	}

	public List findByTgood(Object tgood) {
		return findByProperty(TGOOD, tgood);
	}

	public List findByTbad(Object tbad) {
		return findByProperty(TBAD, tbad);
	}

	public List findByUserId(Object userId) {
		return findByProperty(USER_ID, userId);
	}

	public List findByUserName(Object userName) {
		return findByProperty(USER_NAME, userName);
	}

	public List findByTtype(Object ttype) {
		return findByProperty(TTYPE, ttype);
	}

	public List findAll() {
		log.debug("finding all Tribune instances");
		try {
			String queryString = "from Tribune";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Tribune merge(Tribune detachedInstance) {
		log.debug("merging Tribune instance");
		try {
			Tribune result = (Tribune) getSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Tribune instance) {
		log.debug("attaching dirty Tribune instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Tribune instance) {
		log.debug("attaching clean Tribune instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}